package controller;

import constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Article;
import pojo.Comment;
import pojo.User;
import service.IArticleService;
import service.ICommentService;
import service.IUserService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IArticleService articleService;

    @ResponseBody
    @RequestMapping(value = "/comment/reply/",method = RequestMethod.POST)
    public Map userReply(Comment comment,int total,HttpServletRequest request){
        Map map = new HashMap();
        User user  = (User) request.getSession().getAttribute("user");
//        //构造消息对象
//        Message message = new Message();
        //未登录
        if(user==null){
            map.put("status",1 );
            map.put("msg", "未登录");
            return map;
        }
//        message.setMessageId(user.getUserId());
        //查看评论是的对象是用户还是帖子,是帖子则需重新指定replyUserId
        String commentContent = comment.getCommentContent();
        if(commentContent.startsWith(Constant.START_WITH)){
            int index = commentContent.indexOf("</a>");
            int length = Constant.START_WITH.length();
            String userName = commentContent.substring(length, index);
            User temp = userService.getUserByName(userName);
            comment.setCommentReplyUserId(temp.getUserId());
//            message.setMessageReceptUserId(temp.getUserId());
        }
        //设置评论日期
        comment.setCommentDate(new Date());
        //设置评论访问路径
        String commentUrl = null;
        int pageCurrent = 0;
        if(total%Constant.DEFAULT_PAGE_SIZE_SMALL ==0){
            pageCurrent = total / Constant.DEFAULT_PAGE_SIZE_SMALL + 1;
        }else {
            pageCurrent = total / Constant.DEFAULT_PAGE_SIZE_SMALL;
        }
        commentUrl = "/article/"+comment.getCommentArticleId()+"/page/"+pageCurrent+"/#item";
        comment.setCommentUrl(commentUrl);
        //将评论添加到数据库中
        int i = commentService.addComment(comment);
        if(i==0){
            map.put("status",1 );
            map.put("msg","评论提交失败" );
            return map;
        }
        map.put("status",0 );
        map.put("msg","评论成功" );
        map.put("action",false );
        //增加评论量
        Article article = articleService.getArticleById(comment.getCommentArticleId());
        if(article.getArticleCommentNumber()==null){
            article.setArticleCommentNumber(1);
        }else {
            article.setArticleCommentNumber(article.getArticleCommentNumber()+1);
        }
        articleService.modifyArticleCommentNumber(article);
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/message/nums/",method = RequestMethod.POST)
    public Map userMessages(HttpServletRequest request){
        Map map = new HashMap();
        //获取用户信息
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            map.put("msg", "未登录");
            map.put("status","1" );
            return map;
        }
        Integer userId = user.getUserId();
        //获取用户收到的回复
        List<Comment> comments = commentService.getCommentsByReplyUserIdAndStatus(userId,0);
        map.put("status",0 );
        map.put("count", comments.size());

        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/message/find/",method = RequestMethod.POST)
    public Map findUserMessages(HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        List<Comment> comments = commentService.getCommentsByReplyUserIdAndNotDel(userId);
        for (Comment comment : comments) {
            //根据相应评论获取对应的帖子
            if(comment.getCommentId()!=null){
                Article article = articleService.getArticleById(comment.getCommentArticleId());
                comment.setArticle(article);
            }
        }
        map.put("status",0 );
        map.put("rows",comments );
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/message/read",method = RequestMethod.POST)
    public Map messageRead(HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            map.put("status", 1);
            map.put("msg","未登录" );
            return map;
        }
        Integer userId = user.getUserId();
        int i = commentService.setCommendStatusByReplyUserId(userId, 1);
        map.put("status", 0);
        map.put("action","/user/message/" );
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/message/remove/",method = RequestMethod.POST)
    public Map messageRemove(Integer id,Boolean all,HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        //删除所用
        if(all!=null&&all){
            commentService.setCommendStatusByReplyUserId(user.getUserId(), 2);
            map.put("status", 0);
//            map.put("msg","清空完毕" );
            return map;
        }
        if(id != null){
            Comment comment = new Comment();
            comment.setCommentId(id);
            comment.setCommentStatus((byte) 2);
            int i = commentService.modifyCommentByCommentId(comment);
            if(i==1){
                map.put("status", 0);
                return map;
            }
        }
        map.put("status",1 );
        map.put("msg","消息删除异常" );
        return map;
    }
}
